package com.ruoyi.web.controller.datascreen;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.datascreen.domain.vo.DsElementVo;
import com.ruoyi.datascreen.domain.bo.DsElementBo;
import com.ruoyi.datascreen.service.IDsElementService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 大屏元素
 *
 * @author ruoyi
 * @date 2023-11-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/datascreen/element")
public class DsElementController extends BaseController {

    private final IDsElementService iDsElementService;

    /**
     * 查询大屏元素列表
     */
    @SaCheckPermission("datascreen:element:list")
    @GetMapping("/list")
    public TableDataInfo<DsElementVo> list(DsElementBo bo, PageQuery pageQuery) {
        return iDsElementService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询大屏元素列表全部数据
     *
     * @param bo
     * @return
     */
    @SaCheckPermission("datascreen:element:list")
    @GetMapping("/listAll")
    public R<List<DsElementVo>> listAll(DsElementBo bo){
        return R.ok(iDsElementService.queryList(bo));
    }

    /**
     * 导出大屏元素列表
     */
//    @SaCheckPermission("datascreen:element:export")
//    @Log(title = "大屏元素", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(DsElementBo bo, HttpServletResponse response) {
//        List<DsElementVo> list = iDsElementService.queryList(bo);
//        ExcelUtil.exportExcel(list, "大屏元素", DsElementVo.class, response);
//    }

    /**
     * 获取大屏元素详细信息 带模型信息
     *
     * @param elementId 主键
     */
    @SaCheckPermission("datascreen:element:query")
    @GetMapping("/{elementId}")
    public R<DsElementVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long elementId) {
        DsElementVo dsElementVo = iDsElementService.queryById(elementId);
        if(dsElementVo.getElementModel() != null){
            dsElementVo = iDsElementService.selectElementModel(dsElementVo);
        }
        return R.ok(dsElementVo);
    }

    /**
     * 获取大屏元素详细信息
     *
     * @param elementId
     * @return
     */
    @SaCheckPermission("datascreen:element:query")
    @GetMapping("/info/{elementId}")
    public R<DsElementVo> getInfoForUpdate(@NotNull(message = "主键不能为空")
                                  @PathVariable Long elementId) {
        return R.ok(iDsElementService.queryById(elementId));
    }

    /**
     * 获取大屏元素echarts显示数据
     *
     * @param elementId
     * @return
     */
    @SaCheckPermission("datascreen:element:query")
    @GetMapping("/echarts/{elementId}")
    public R getEchartsInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long elementId) throws ClassNotFoundException {
        return R.ok(iDsElementService.selectEchartsInfoById(elementId));
    }

    /**
     * 新增大屏元素
     */
    @SaCheckPermission("datascreen:element:add")
    @Log(title = "大屏元素", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DsElementBo bo) {
        return toAjax(iDsElementService.insertByBo(bo));
    }

    /**
     * 修改大屏元素
     */
    @SaCheckPermission("datascreen:element:edit")
    @Log(title = "大屏元素", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DsElementBo bo) {
        return toAjax(iDsElementService.updateByBo(bo));
    }

    /**
     * 删除大屏元素
     *
     * @param elementIds 主键串
     */
    @SaCheckPermission("datascreen:element:remove")
    @Log(title = "大屏元素", businessType = BusinessType.DELETE)
    @DeleteMapping("/{elementIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] elementIds) {
        return toAjax(iDsElementService.deleteWithValidByIds(Arrays.asList(elementIds), true));
    }
}
